package com.magicborrow.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.magicborrow.R;

/**
 * Created by ronaldo on 2016/6/2.
 */
public class SelectSexDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private Button btn_cancel;
    private Button btn_confirm;
    private TextView tv_content;
    private NumberPicker np_sex;
    private String[] sexs;

    public SelectSexDialog(Context context) {
        super(context, R.style.picker_style);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_sex);
        initView();
        initData();
        initListener();
        setDialogSize(1d);
    }

    private String temp="";
    private void initListener() {
        btn_cancel.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

        np_sex.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                temp=sexs[newVal];

            }
        });
    }
    private void initData() {
        tv_content.setText("性别");
        sexs = new String[]{"男","女"};
        np_sex.setDisplayedValues(sexs);
        np_sex.setMinValue(0);
        np_sex.setMaxValue(sexs.length - 1);
    }

    public String getSex(){
        if(temp.equals("")){
            temp="男";
        }
        return temp;
    }

    private void initView() {
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        tv_content = (TextView) findViewById(R.id.tv_content);
        np_sex = (NumberPicker) findViewById(R.id.np_sex);
    }

    private SexButtonListener listener;
    public void setSexButtonListener(SexButtonListener listener) {
        this.listener = listener;
    }
    public interface SexButtonListener {
        void onPositiveButton(SelectSexDialog dialog);
        void onNegativeButton(SelectSexDialog dialog);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                listener.onPositiveButton(this);
                break;
            case R.id.btn_cancel:
                listener.onNegativeButton(this);
                break;
            default:
                break;
        }
    }

    public void setDialogSize(double percent){
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mystyle);  //添加动画
        Display play = window.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        play.getMetrics(metrics);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=(int) ((metrics.widthPixels)*percent);
        params.height=params.WRAP_CONTENT;
        onWindowAttributesChanged(params);
        window.setAttributes(params);
    }
}

